package com.example.ui_test.ui

import android.media.Image
import android.os.Environment
import android.util.Log
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.example.ui_test.R
import com.kun.base.ui.AbsBaseActivity
import com.taibo.giflib.view.GifView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

/**
 *
 *
 * @Author: BigWhite
 * @CreateDate: 2021/4/27 11:40
 */
@Route(path = "/uiTest/UITestActivity")
class UITestActivity :AbsBaseActivity() {
    override fun getLayoutId(): Int {
        return R.layout.activity_ui_test
    }

    override fun initViewModel() {
    }

    override fun initView() {
        val gifView = findViewById<GifView>(R.id.gifView)
        val imageView = findViewById<ImageView>(R.id.image_view)

//        copyAssetsFile2Phone("fl.gif"){
//            gifView.setGifPath(it)
//        }
        Glide.with(this).load("file:///android_asset/fl.gif").into(imageView)
    }

    fun copyAssetsFile2Phone( fileName: String, call: ((url: String) -> Unit)) {
        GlobalScope.launch {
            val url =
                getExternalFilesDir(Environment.DIRECTORY_PODCASTS)!!.absolutePath + File.separator + fileName
            try {
                val inputStream = assets.open(fileName)
                //getFilesDir() 获得当前APP的安装路径 /data/data/包名/files 目录
                val file = File(url)
                if (!file.exists() || file.length() == 0L) {
                    val fos = FileOutputStream(file) //如果文件不存在，FileOutputStream会自动创建文件
                    var len = -1
                    val buffer = ByteArray(1024)
                    while (inputStream.read(buffer).also { len = it } != -1) {
                        fos.write(buffer, 0, len)
                    }
                    fos.flush() //刷新缓存区
                    inputStream.close()
                    fos.close()
                    Log.e("copy---", "模型文件复制完毕")
                } else {
                    Log.e("copy---", "模型文件已存在，无需复制")
                }
            } catch (e: IOException) {
                e.printStackTrace()
            }

            GlobalScope.launch(Dispatchers.Main){
                call(url)
            }
        }
//        val doOnNext = Observable.create(ObservableOnSubscribe<String> { emitter ->
//            val url =
//                getExternalFilesDir(Environment.DIRECTORY_PODCASTS)!!.absolutePath + File.separator + fileName
//            try {
//                val inputStream = assets.open(fileName)
//                //getFilesDir() 获得当前APP的安装路径 /data/data/包名/files 目录
//                val file = File(url)
//                if (!file.exists() || file.length() == 0L) {
//                    val fos = FileOutputStream(file) //如果文件不存在，FileOutputStream会自动创建文件
//                    var len = -1
//                    val buffer = ByteArray(1024)
//                    while (inputStream.read(buffer).also { len = it } != -1) {
//                        fos.write(buffer, 0, len)
//                    }
//                    fos.flush() //刷新缓存区
//                    inputStream.close()
//                    fos.close()
//                    Log.e("copy---", "模型文件复制完毕")
//                } else {
//                    Log.e("copy---", "模型文件已存在，无需复制")
//                }
//            } catch (e: IOException) {
//                e.printStackTrace()
//            }
//            emitter.onNext(url)
//        }).subscribeOn(Schedulers.io())
//            .observeOn(AndroidS.mainThread())
//            .doOnNext {
//                call(it)
//            }.subscribe()
    }
}